// pages/childFollowUp/childFollowUpDetail/childFollowUpDetail.js
const app = getApp();
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ptTaskInfoSearchVo: null,
    defaultDate: util.formatDate(new Date((new Date().getTime() + 24 * 3600 * 1000))),
    startTime: null,
    endTime: null,
    intervalTime: null,
    executeTime: null,
    noMessage: false,
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '下次门诊检查提醒详情', //导航栏 中间的标题
    },
    navHeight: app.globalData.statusBarHeight * 2 + 86,
    defaultTime: "08:30"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function(){
      wx.request({
        url: app.getUrl('/xcx/pt/followUpDetail'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        data: {
          messageId: options.id
        },
        success(res) {
          if (res.data.code == 0) {
            that.setData({
              messageId: options.id,
              ptTaskInfoSearchVo: res.data.data,
              //获取后台的windowDay和executeTime
              intervalTime: res.data.data.windowDay,
              executeTime: res.data.data.executeTime,
            })
            //计算下次门诊提醒时间选择区间
            var datetosolve = that.dateToSolve(that.data.executeTime, that.data.intervalTime)
            //初始化开始时间和结束时间
            that.setData({
              startTime: that.compareDate(util.formatDate(new Date()), datetosolve[0]),
              endTime: util.formatDate(datetosolve[1])
            })
            if (res.data.data.message.updateBy) {
              that.setData({
                defaultDate: res.data.data.message.updateDate.substring(0, 10),
                defaultTime: res.data.data.message.updateDate.substring(11)
              })
            }
          } else if (res.data.code == 404) {
            that.setData({
              noMessage: true
            })
          }
          wx.hideLoading();
        }
      })
    },2000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDateChange: function (e) {
    this.setData({
      defaultDate: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      defaultTime: e.detail.value
    })
  },
  saveUpdateDate: function () {
    var that = this
    if (that.data.defaultDate == "请选择") {
      wx.showToast({
        title: '请选择到院检查时间',
        icon: 'none'
      })
      return false;
    }
    wx.request({
      url: app.getUrl('/xcx/pt/saveUpdateDate'),
      header: wx.getStorageSync('cookie'),
      data: {
        messageId: that.data.messageId,
        updateDate: that.data.defaultDate + " " + that.data.defaultTime,
      },
      success(res) {
        if (res.data.code == 0) {
          wx.switchTab({
            url: '../../message/message',
          })
        }
      }
    })
  },
  /**
   * 通过executeTime和windowDay计算下次门诊时间区间
   */
  dateToSolve(executeTime, windowDay) {
    var execute = new Date(executeTime)
    var executeDateAdd = new Date(execute.getTime() + windowDay * 24 * 3600 * 1000)
    var executeDateReduce = new Date(execute.getTime() - windowDay * 24 * 3600 * 1000)
    return [executeDateReduce, executeDateAdd];
  },
  /**
   * 比较两个时间大小，返回较大值
   */
  compareDate(date1, date2) {
    var sdate1 = new Date(date1)
    var sdate2 = new Date(date2)
    return sdate1.getTime() > sdate2.getTime() ? date1 : date2
  }
})